package com.wxn.learncompose.pagers

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
fun MyBottomNavigateBar(selectedIndex: MutableState<Int>){
    val menuItems = remember{ mutableListOf(
        "首页" to Icons.Filled.Home,
        ""
    ) }
    var interactionSource by remember {
        mutableStateOf(MutableInteractionSource())
    }


    NavigationBar(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        for (index in 0 until  menuItems.size) {
            Column(
                modifier = Modifier
                    .fillMaxHeight()
                    .weight(1f)
                    .clickable(
                        onClick = {},
                        indication = null,
                        interactionSource = interactionSource
                    ),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                NavigationIcon(index, selectedIndex)
                Spacer(modifier = Modifier.height(5.dp))
                AnimatedVisibility(visible = (index == selectedIndex.value)) {
                    Surface(
                        shape = CircleShape,
                        modifier = Modifier.size(5.dp),
                        color = Color(0xFF252927)
                    ) {}
                }
            }
        }
    }
}

@Composable fun NavigationIcon(index: Int, selectedIndex: MutableState<Int>) {
    val alpha = if (index == selectedIndex.value) 0.5f else 1f
    CompositionLocalProvider {
        when(index) {
            0 -> Icon(Icons.Filled.Home, null)
            1 -> Icon(Icons.Filled.Favorite, null)
            else -> Icon(Icons.Filled.Settings, null)
        }
    }
}
